package com.controller;

import cn.dsna.util.images.ValidateCode;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
@WebServlet("/code")
public class CodeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ValidateCode validateCode=new ValidateCode(120,40,4,30);
        String code = validateCode.getCode();//获取到生成的验证码的字符串内容
        req.getSession().setAttribute("code",code);//将当前的验证码保存到 session 中,用于后面的校验
        System.err.println("验证码是:"+code);
        BufferedImage buffImg = validateCode.getBuffImg();//获取到生产的图片
        ImageIO.write(buffImg,"jpeg",resp.getOutputStream());
    }
}
